package com.dora.algorithm.hsbc;


/*

  An agent sends a secret message to headquarters containing the details of his project,
  He sends ane soft copy to the agency's computer (P) and sends one hardcopy by fax to
  Roger, the technical head of the agency (Q). But during the transmission,noise in the
  network causes some bits of the data message P to get distorted. However, we know that
  Roger always matches the binary values of both messages and checks whether he can
  convert the message p to message Q by flipping the minimum number of bits.

  Write an algorithm to help Roger find the minimum number of bits that must be flipped to
  convert message P to message Q.

  input
  The first line of the input consists of an integer num1, representing the secret message
  sent to the agency's computer (P).The second line consists of an integer num2,representing
  the message sent to the technical head of the agency (Q).

  Output
  Print an integer representing the minimum number of bits that must be flipped to convert
  message p to message Q.


  Constraints
  -10^4 <= num1, num2 <= 10^9

  Example
  Input
  7
  10

  Output
  3

  Explanation:
  Binary representation of p is 00000111.
  Binary representation of Q is 00001010.
  Three bits of p at position 5, 6 and 8 must be flipped.
  so the number of bits that must be flipped is 3

  */
import java.util.*;

public class HammingDistanceSolution {


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 读取两个整数
        int num1 = in.nextInt();
        int num2 = in.nextInt();
        in.close();

        // 计算最小翻转次数并输出
        System.out.println(flippedBits(num1, num2));
    }

    /**
     * 翻转位
     *
     * @param num1 num1
     * @param num2 num2
     * @return int
     */
    public static int flippedBits(int num1, int num2) {
        // 计算 num1 和 num2 的异或结果
        int xorResult = num1 ^ num2;
        // 统计 xorResult 中 1 的个数，即不同的位数
        return Integer.bitCount(xorResult);
    }

    /**
     * 手动翻转位运算
     *
     * @param num1 num1
     * @param num2 num2
     * @return int
     */
    public static int flippedBitsByHand(int num1, int num2) {
        int xorResult = num1 ^ num2; // 计算异或结果
        int count = 0;

        while (xorResult != 0) {
            count += xorResult & 1; // 检查最低位是否为1
            xorResult >>= 1; // 右移去掉最低位
        }

        return count;
    }

}

